#!/usr/bin/env bash

ack '(?<=\#define)\s*DDEBUG\s*[1-9]' src && exit 1
ack '.{81}' `ls src/sregex/sre_*.[ch]|grep -v yyparser|grep -v _x64\.h` src/sregex/sre_yyparser.y
ack '(?<!:)//' `ls src/sregex/sre_*.[ch]|grep -v _x64\.h`
ack '^master_on' t && exit 1
ack '\b(?:ONLY|LAST)\b' t
ack -l '\r\n' t
ack '\(\);' `ls src/sregex/sre_*.h|grep -v yyparser`
ack '^static .*?\(\);' `ls src/sregex/sre_*.[ch]|grep -v yyparser`
perl -e 'use strict; use warnings;
    my $seen = 0;
    my $saved;
    my $indent;
    for my $fname (@ARGV) {
        open my $in, $fname or die $!;
        while (<$in>) {
            next if /^\s*$/ || m{^\s*/\*} || m{^\#\s*include\s+};
            if (/(sre_p[cn]*alloc\b|\b[mc]alloc\b|\bsre_array_push\b|\bsre_regex_create\b|\bsre_alloc_chain_link\b|\bsre_c?alloc_buf\b)\(/) {
                my $pattern = $1;
                if (/^\Q$pattern\E/) {
                    next;
                }
                if (!/return\b.*?$pattern/ && !/"[^"]*$pattern/ && !/^\s+\*/) {
                    if (/^\s+/) {
                        $indent = $&;
                    } else {
                        $indent = "";
                    }
                    $seen = 1;
                    $saved = $_;
                    next;
                }
            }
            if ($seen) {
                if (/^\s+/) {
                    if (length($&) > length($indent)) {
                        next;
                    }

                } elsif (/^\#else/) {
                    undef $seen;
                    next;

                } elsif (/^\#endif$/) {
                    next
                }

                if (!/\bif\b.*? [=!]= NULL/) {
                    warn "not check NULL: $fname: line $.: $saved$_";
                }
            }
            $seen = 0;
        }
        close $in;
    }' `ls src/sregex/sre_*.[cy] src/*.c`

ack '_log_error\(NGX_[^L]' src/sregex/sre_*.[ch]
ack '\#define\s+SRE_USE_VALGRIND\s+[1-9]' src/sregex/sre_core.h

echo done.

